#!/bin/bash

# general config
. /etc/rc.conf
. /etc/rc.d/functions

PID=$(ps aux | grep '[/]opt/ude/bin/pam-helper/main.tac' | awk '{print $2}')
case "$1" in
  start)
    stat_busy "Starting ude-pam-helper"
    [ -z "$PID" ] && /usr/bin/twistd --python=/opt/ude/bin/pam-helper/main.tac --logfile=/var/log/ude-pam-helper.log --pidfile=/var/run/ude-pam-helper.pid
    if [ $? -gt 0 ]; then
      stat_fail
    else
      add_daemon ude-pam-helper
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping ude-pam-helper"
    [ ! -z "$PID" ]  && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm_daemon ude-pam-helper
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 2
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0
